package com.acme.bankapp.services;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class DataWorker {

	private final File file;

	public DataWorker(String pathname) throws FileNotFoundException{
		this.file = new File(pathname);
		if(!this.file.exists()) throw new FileNotFoundException(file.getAbsolutePath() + " not exist");
	}

	public void save(Serializable object) throws IOException {
		try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(
				new BufferedOutputStream(new FileOutputStream(file)))) {

			objectOutputStream.writeObject(object);
		}
	}

	public Object restore() throws IOException, ClassNotFoundException {
		try (ObjectInputStream objectOutputStream = new ObjectInputStream(
				new BufferedInputStream(new FileInputStream(file)))) {
			return objectOutputStream.readObject();
		}
	}

}
